package $2_1oo;

public class Functions {

	public static void main(String[] args) {
		// 面向对象
		Person ming = new Person();
        ming.setName("Xiao Ming"); // 对字段name赋值
        ming.setAge(12); // 对字段age赋值
        ming.setBirth(2008);
        System.out.println(ming.getName() + ", " + ming.getAge());
        System.out.println(ming.getAgeFromBirth());
        
        
        Person lisi = new Person();
        lisi.setNameAndAge("lisi", 22);
        System.out.println(lisi.getName() + ", " + lisi.getAge());
        
        Group g = new Group();
        g.setNames("Xiao Ming", "Xiao Hong", "Xiao Jun"); // 传入3个String
        g.setNames("Xiao Ming", "Xiao Hong"); // 传入2个String
        g.setNames("Xiao Ming"); // 传入1个String
        g.setNames(); // 传入0个String
        
        Persona p = new Persona();
        String[] fullname = new String[] { "Homer", "Simpson" };
        p.setName(fullname); // 传入fullname数组
        System.out.println(p.getName()); // "Homer Simpson"
        fullname[0] = "Bart"; // fullname数组的第一个元素修改为"Bart"
        System.out.println(p.getName()); // "Homer Simpson"还是"Bart Simpson"? Bart Simpson
        
        Personb pb = new Personb();
        String bob = "Bob";
        pb.setName(bob); // 传入bob变量
        System.out.println(pb.getName()); // "Bob"
        bob = "Alice"; // bob改名为Alice
        System.out.println(pb.getName()); // "Bob"还是"Alice"?
        
	}

}

class Person {
	private String name;
	private int age;
	private int birth;
	
	public String getName() {
        return name;	// 相当于this.name
    }

    public void setName(String name) {
    	if (name == null || name.isBlank()) {
            throw new IllegalArgumentException("invalid name");
        }
        this.name = name.strip(); // 去掉首尾空格	// 前面的this不可少，少了就变成局部变量name了
    }

    public void setAge(int age) {
        if (age < 0 || age > 100) {
            throw new IllegalArgumentException("invalid age value");
        }
        this.age = age;
    }
    public void setBirth(int birth) {
        this.birth = birth;
    }

    public int getAge() {
        return this.age; // 调用private方法
    }
    public int getAgeFromBirth() {
        return calcAge(2021); // 调用private方法
    }

    // private方法:
    private int calcAge(int currentYear) {
        return currentYear - this.birth;
    }
    
    // 方法参数
    public void setNameAndAge(String name, int age) {
    	setName(name);
    	setAge(age);
    }
}

class Persona {
    private String[] name;

    public String getName() {
        return this.name[0] + " " + this.name[1];
    }

    public void setName(String[] name) {
        this.name = name;
    }
}

class Personb {
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Personc {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Group {
    private String[] names;

    // 可变参数
    // 可变参数用类型...定义，可变参数相当于数组类型
    public void setNames(String... names) {
        this.names = names;
    }
}